package com.zh.blog.controller.article;

import com.zh.blog.annotation.OptLog;
import com.zh.blog.common.api.Result;
import com.zh.blog.dto.*;
import com.zh.blog.enums.FilePathEnum;
import com.zh.blog.service.ArticleService;
import com.zh.blog.strategy.context.UploadStrategyContext;
import com.zh.blog.vo.ArticleVO;
import com.zh.blog.vo.ConditionVO;
import com.zh.blog.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import java.util.List;

import static com.zh.blog.constant.OptTypeConst.SAVE_OR_UPDATE;

/**
 * 发布文章控制器
 * @author ZhengHao
 * Created At 2022/8/4.
 **/
@Api(tags = "发布文章模块")
@RestController
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UploadStrategyContext uploadStrategyContext;

    /**
     * 上传文章图片
     * @param file 图片文件
     * @return 文章图片地址
     */
    @ApiOperation(value = "上传文章图片")
    @ApiImplicitParam(name = "file", value = "文章图片",required = true, dataType = "MultipartFile")
    @PostMapping("/admin/articles/images")
    public Result<String> saveArticleImages(MultipartFile file){
        return Result.ok(uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.ARTICLE.getPath()));
    }

    /**
     * 添加或者修改文章
     * @param articleVO 文章信息
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或者修改文章")
    @PostMapping("/admin/articles")
    public Result<?> saveOrUpdateArticles(@Valid @RequestBody ArticleVO articleVO){
        articleService.saveOrUpdateArticles(articleVO);
        return Result.ok();
    }

    /**
     * 查看首页文章
     *
     * @return 首页文章列表
     */
    @ApiOperation(value = "查看首页文章")
    @GetMapping("/articles")
    public Result<List<ArticleHomeDto>> listArticles() {
        return Result.ok(articleService.listArticles());
    }

    /**
     * 点赞文章
     * @param articleId 文章ID
     * @return
     */
    @ApiOperation(value = "点赞文章")
    @ApiImplicitParam(name = "articleId", value = "文章ID", required = true, dataType = "Integer")
    @PostMapping("/articles/{articleId}/like")
    public Result<?> saveArticleLike(@PathVariable("articleId") Integer articleId) {
        articleService.saveArticleLike(articleId);
        return Result.ok();
    }

    /**
     * 根据ID查看文章
     * @param articleId 文章ID
     * @return 文章信息
     */
    @ApiOperation(value = "根据ID查看文章")
    @ApiImplicitParam(name = "articleId",value = "文章ID", required = true, dataType = "Integer")
    @GetMapping("/articles/{articleId}")
    public Result<ArticleDto> getArticleById(@PathVariable("articleId") Integer articleId) {
        return Result.ok(articleService.getArticleById(articleId));
    }

    /**
     * 搜索文章
     * @param conditionVO 搜索条件
     * @return 文章列表
     */
    @ApiOperation(value = "搜索文章")
    @GetMapping("/articles/search")
    public Result<List<ArticleSearchDto>> listArticlesBySearch(ConditionVO conditionVO) {
        return Result.ok(articleService.listArticlesBySearch(conditionVO));
    }

    /**
     * 查看文章归档
     * @return 文章归档列表
     */
    @ApiOperation(value = "查看文章归档")
    @GetMapping("/articles/archives")
    public Result<PageResult<ArchiveDto>> listArchives() {
        return Result.ok(articleService.listArchives());
    }

    /**
     * 根据条件查询文章
     * @param conditionVO 条件
     * @return 文章列表
     */
    @ApiOperation(value = "根据条件查询文章")
    @GetMapping("/articles/condition")
    public Result<ArticlePreviewListDto> listArticlesByCondition(ConditionVO conditionVO) {
        return Result.ok(articleService.listArticlesByCondition(conditionVO));
    }
}